package com.example.demo.Service;

import com.example.demo.Mappers.RegistMapper;
import com.example.demo.beans.*;
import com.example.demo.utils.AppConfigUtils;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by Administrator on 2018/4/7.
 */
@Service("RegistPatientService")
public class RegistPatientService {
    @Autowired
    private RegistMapper registMapper;
    /****当病人已经注册就诊卡时候执行***/
    @Transactional(readOnly = true)
    public RegistPatientInfoResponse ResponseWithIspatient(RegistPatientInfoRequest request){
     registMapper.saveregist(request);

        return  registMapper.getRegistPoseByid(request.getPatientID());
    }
    /**当患者通过其他卡注册时候执行*/
    public  RegistPatientInfoResponse ResponseByOtherCard(RegistPatientInfoRequest request){
        RegistPatientInfoResponse response=new RegistPatientInfoResponse();
        Brzc_YwxhBean brzc_ywxhBean=registMapper.getBrzcYwxh();
        Ghxh_YwckBean ghxh_ywckBean=registMapper.getGhxhYwxh();
        RegistOldBean oldBean=registMapper.getpainfo(request.getCardNo());
        if(oldBean!=null){
            RegistPatientInfoResponse re =new RegistPatientInfoResponse();
            re.setPatientID(oldBean.getPatientID());
            re.setHosTranNo("无流水号");
            re.setResultCode("1");
            re.setOpDateTime(oldBean.getOpDateTime());
            re.setResultMsg("该卡已绑定无需绑卡");
            return  re;
        }
        else{
            //当身份整校验不通过时候执行如下代码
            if (DateUtils.isIDNumber(request.getIDCardNo())==false){
                RegistPatientInfoResponse re =new RegistPatientInfoResponse();
                re.setResultCode("1");
                re.setResultMsg("身份证校验错误,请检查，给您带来不便敬请谅解，如不能绑定请移步收费窗口挂号！");
                return  re;
            }

            //病人注册表中业务序号相差天数
            int k= DateUtils.daysBetween(brzc_ywxhBean.getSsrq());
            //病人挂号表中业务序号相差天数
            int m=DateUtils.daysBetween(ghxh_ywckBean.getSsrq());
            Long tempbrid=Long.parseLong(DateUtils.getStringToday()+"000000");
            Long tempghxh=Long.parseLong(DateUtils.getStringToday()+"000000");
            String ywckbm= AppConfigUtils.getYwckbm();
            String czybm=AppConfigUtils.getGhybm();
            String brnl= String.valueOf(DateUtils.getAgeByIdCard(request.getIDCardNo()));
            String czyks=AppConfigUtils.getGhyks();
            String ghzlbm=AppConfigUtils.getGhzlbm();
            String ghfsbm=AppConfigUtils.getGhfsbm();
            String zcrq=DateUtils.Getdnowdatexx();
            String ylkh=request.getCardNo();
            String brxb="";
            String brid="";
            String ghxh="";
            String xb="";
            String fbbm=AppConfigUtils.getFbbm();
            xb=DateUtils.getBirAgeSex(request.getIDCardNo().trim()).get("sexCode");
            if(xb.equals("F")){
                brxb="2";
            }
            else{brxb="1";}
//当注册日期为当天第一个时执行
            if(k>=1&&m>=1){
                System.out.print("k>=1&&m>=1"+"病人注册日期和病人挂号日期与系统里面日期相差都大于1时候执行");
                brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
                ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
                response.setResultCode("0");
                response.setResultMsg("建卡成功!");
                response.setPatientID(brid);
                response.setOpDateTime(zcrq);
                response.setHosTranNo(ghxh);
                PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
                //当查询到患者信息为空时候直接返回
                if(d==null){
                    RegistPatientInfoResponse   r=new RegistPatientInfoResponse();
                    r.setResultCode("1");
                    r.setResultMsg("提示：获取病人信息为空，请检查传入的参数！");
                    return r;
                }
//                PayDetailBean pay=setPayBean(d,jsjlid,request);
//                registMapper.saverePaybean(pay);
                return response;
            }

            else if(k>=1&&m<1){
                System.out.print("k>=1&&m<1"+"病人注册日期大于1，挂号日期小于1时候执行");
                brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
                ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
                response.setResultCode("0");
                response.setResultMsg("建卡成功!");
                response.setPatientID(brid);
                response.setOpDateTime(zcrq);
                response.setHosTranNo(ghxh);
                PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
                //当查询到患者信息为空时候直接返回
                if(d==null){
                    RegistPatientInfoResponse   r=new RegistPatientInfoResponse();
                    r.setResultCode("1");
                    r.setResultMsg("提示：获取病人信息为空，请检查传入的参数！");
                    return r;
                }
//                PayDetailBean pay=setPayBean(d,jsjlid,request);
//                registMapper.saverePaybean(pay);
                return response;

            }

            else if(k<1&&m>=1){
                System.out.print("k<1&&m>=1"+"病人注册日期小于1，挂号日期大于1时候执行");
                brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
                ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
                response.setResultCode("0");
                response.setResultMsg("建卡成功!");
                response.setPatientID(brid);
                response.setOpDateTime(zcrq);
                response.setHosTranNo(ghxh);
                PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
                //当查询到患者信息为空时候直接返回
                if(d==null){
                    RegistPatientInfoResponse   r=new RegistPatientInfoResponse();
                    r.setResultCode("1");
                    r.setResultMsg("提示：获取病人信息为空，请检查传入的参数！");
                    return r;
                }
//                PayDetailBean pay=setPayBean(d,jsjlid,request);
//                registMapper.saverePaybean(pay);
                return response;

            }
            else{
                System.out.print("k<1&&m<1"+"病人注册日期小于1，挂号日期小于1时候执行");
                brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
                ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
                response.setResultCode("0");
                response.setResultMsg("建卡成功!");
                response.setPatientID(brid);
                response.setOpDateTime(zcrq);
                response.setHosTranNo(ghxh);
                PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
                //当查询到患者信息为空时候直接返回
                if(d==null){
                    RegistPatientInfoResponse   r=new RegistPatientInfoResponse();
                    r.setResultCode("1");
                    r.setResultMsg("提示：获取病人信息为空，请检查传入的参数！");
                    return r;
                }
//                PayDetailBean pay=setPayBean(d,jsjlid,request);
//                registMapper.saverePaybean(pay);
                return response;

            }
        }
    }



    /**当病人未绑定就诊卡时候执行**/
    @Transactional(readOnly = true)
    public RegistPatientInfoResponse RensonseWithNopatient(RegistPatientInfoRequest request){
        Brzc_YwxhBean brzc_ywxhBean=registMapper.getBrzcYwxh();
        Ghxh_YwckBean ghxh_ywckBean=registMapper.getGhxhYwxh();
        Jsjl_YwxhBean jsjl_ywxhBean=registMapper.getJsjlywxh();
RegistOldBean oldBean=registMapper.getpainfo(request.getCardNo());
if(oldBean!=null){
    RegistPatientInfoResponse re =new RegistPatientInfoResponse();
    re.setPatientID(oldBean.getPatientID());
    re.setHosTranNo("无流水号");
    re.setResultCode("1");
    re.setOpDateTime(oldBean.getOpDateTime());
    re.setResultMsg("该卡已绑定无需绑卡");
    return  re;
}

if (DateUtils.isIDNumber(request.getIDCardNo())==false){
    RegistPatientInfoResponse re =new RegistPatientInfoResponse();
    re.setResultCode("1");
    re.setResultMsg("身份证校验错误,请检查，给您带来不便敬请谅解，如不能绑定请移步收费窗口挂号！");
     return  re;
}
      //病人注册表中业务序号相差天数
        int k= DateUtils.daysBetween(brzc_ywxhBean.getSsrq());
        //病人挂号表中业务序号相差天数
        int m=DateUtils.daysBetween(ghxh_ywckBean.getSsrq());
        //病人结算表中业务序号相差天数
        int i=DateUtils.daysBetween(jsjl_ywxhBean.getSsrq());
        Long tempbrid=Long.parseLong(DateUtils.getStringToday()+"000000");
        Long tempghxh=Long.parseLong(DateUtils.getStringToday()+"000000");
        Long tempjsjlid=Long.parseLong(DateUtils.getStringToday()+"000000");
        String ywckbm= AppConfigUtils.getYwckbm();
        String czybm=AppConfigUtils.getGhybm();
        String czyxm=AppConfigUtils.getCzyxm();
        String brnl= String.valueOf(DateUtils.getAgeByIdCard(request.getIDCardNo()));
        String czyks=AppConfigUtils.getGhyks();
        String ghzlbm=AppConfigUtils.getGhzlbm();
        String ghfsbm=AppConfigUtils.getGhfsbm();
        String fbbm=AppConfigUtils.getFbbm();
        String zcrq=DateUtils.Getdnowdatexx();
        String mxfyxmbm="080137";
        String dlbm="15";
        String xlbm="17";
        String ylkh=request.getCardNo();
        String brxb="";
        String brid="";
        String ghxh="";
        String  jsjlid="";
        String brsr="";
        brsr=DateUtils.getBirAgeSex(request.getIDCardNo()).get("birthday");
        //门诊发票信息
        MzdqpjBean mzdqpjBean=registMapper.Getmzpjxx(czybm);
        String qshnew=String.valueOf(Long.valueOf(mzdqpjBean.getQsh())+1);
        if(request.getSex().equals("男")){brxb="1";}else {brxb="2";}
RegistPatientInfoResponse registPatientInfoResponse =new RegistPatientInfoResponse();
//当注册日期为当天第一个时执行
        if(k>=1&&m>=1&&i>=1){
            System.out.print("k>=1&&m>=1&&i>=1");
            brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
             System.out.print("执行挂号");
             ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
             jsjlid=ifithanone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh);
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
           //当查询到患者信息为空时候直接返回
            if(d==null){
                RegistPatientInfoResponse   r=new RegistPatientInfoResponse();
                r.setResultCode("1");
                r.setResultMsg("提示：获取病人信息为空，请检查传入的参数！");
                return r;
            }
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse;
   }
   else if(k>=1&&m>=1&&i<1){
            System.out.print("k>=1&&m>=1&&i<1");
            brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
            System.out.print("执行挂号");
            ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
            jsjlid=ifilessone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh,jsjl_ywxhBean.getXh());
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse;
        }
        else if(k>=1&&m<1&&i>=1){
            System.out.print("k>=1&&m<1&&i>=1");
            brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
            ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
            jsjlid=ifithanone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh);
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse;
        }
else if(k>=1&&m<1&&i<1){
            System.out.print("k>=1&&m<1&&i<1");
            brid=ifkthanone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,request);
            ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
            jsjlid=ifilessone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh,jsjl_ywxhBean.getXh());
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse;}
        else if(k<1&&m>=1&&i>=1){
            System.out.print("k<1&&m>=1&&i>=1");
            brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
            ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
            jsjlid=ifithanone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh);
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse; }
   else if(k<1&&m>=1&&i<1){
            System.out.print("k<1&&m>=1&&i<1");
            brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
            ghxh=ifmthanone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm);
            jsjlid=ifilessone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh,jsjl_ywxhBean.getXh());
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse; }
        else if(k<1&&m<1&&i>=1){
            System.out.print("k<1&&m<1&&i>=1");
            brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
            ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
            jsjlid=ifithanone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh);
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid);
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse; }
        else {
            System.out.print("当所有小于1时执行到这里");
            brid=ifklessone(brid,tempbrid,czybm,ywckbm,brxb,brnl,zcrq,czyks,brzc_ywxhBean.getXh(),request);
            ghxh=ifmlessone(tempghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,brnl,zcrq,ywckbm ,ghxh_ywckBean.getXh());
            jsjlid=ifilessone(tempjsjlid,ghxh,czybm,ywckbm,brid,czyks,ylkh,jsjl_ywxhBean.getXh());
            registMapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            registMapper.savemzbbrfy(fbbm,ywckbm,xlbm,mxfyxmbm,czybm,dlbm,zcrq,brid,ghxh,request.getName(),"1",
                    "2","2","NULL","NULL",qshnew,jsjlid,"1","1");
            registPatientInfoResponse.setResultCode("0");
            registPatientInfoResponse.setResultMsg("建卡成功!");
            registPatientInfoResponse.setPatientID(brid);
            registPatientInfoResponse.setOpDateTime(zcrq);
            registPatientInfoResponse.setHosTranNo(jsjlid );
            PatientInfodetail d=registMapper.selectpatientdetal(brid,request.getCardNo());
            PayDetailBean pay=setPayBean(d,jsjlid,request,czybm,czyxm);
            registMapper.saverePaybean(pay);
            return registPatientInfoResponse;
        }
       // return  null;
    }


/**当注册日期大于当前注册日期时**/
    private  String  ifkthanone( String brid,Long tempbrid,String czybm,String ywckbm ,
                               String brxb,String brnl,String zcrq,String czyks,RegistPatientInfoRequest request){
        registMapper.updatebrzcywxh("000001",DateUtils.Getdnowdatexx());
        brid=String.valueOf(tempbrid+1);
        registMapper.savebrzcb(brid,czybm,ywckbm,request.getName(),brxb,
                DateUtils.getBirAgeSex(request.getIDCardNo()).get("birthday"),brnl,request.getIDCardNo(),request.getAddress(),
                request.getMobile(),zcrq);
        registMapper.savergistylk(request.getCardNo(),brid,czybm,request.getCardType(),czyks,zcrq);
return brid;
    }

   /**当注册日期等于当前注册日期时**/
    private  String  ifklessone( String brid,Long tempbrid,String czybm,String ywckbm ,
                                 String brxb,String brnl,String zcrq,String czyks,String xh,RegistPatientInfoRequest request){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=registMapper.selectywxhbyid(xh1).getXh();
        registMapper.updatebrzcywxh(xh2,DateUtils.Getdnowdatexx());
        brid=String.valueOf(tempbrid+xh1);
        registMapper.savebrzcb(brid,czybm,ywckbm,request.getName(),brxb,
                DateUtils.getBirAgeSex(request.getIDCardNo()).get("birthday"),brnl,request.getIDCardNo(),request.getAddress(),
                request.getMobile(),zcrq);
        registMapper.savergistylk(request.getCardNo(),brid,czybm,request.getCardType(),czyks,zcrq);
        return brid;
    }

    /**
     * 当挂号业务序号表大于1
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     */
    private  String ifmthanone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
    String czybm,String czyks,String brnl,String zcrq,String ywckbm){
        registMapper.updateghxhywxh("000001",DateUtils.Getdnowdatexx());
      String  ghxh=String.valueOf(tempghxh+1);
        registMapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
                "0015",brnl,zcrq,zcrq,ywckbm,"NULL");
        return  ghxh;
    }

    /**
     * 当挂号时间等于当天时间
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     * @param xh
     * @return
     */
    private  String ifmlessone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
                               String czybm,String czyks,String brnl,String zcrq,String ywckbm,String xh){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=registMapper.selectywxhbyid(xh1).getXh();
        registMapper.updateghxhywxh(xh2,DateUtils.Getdnowdatexx());
        String  ghxh=String.valueOf(tempghxh+xh1);
        registMapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
                "0015",brnl,zcrq,zcrq,ywckbm,"NULL");
        return  ghxh;
    }

    /**
     * 当结算记录业务序号时间大于1时执行
     * @param tempjsjlid
     * @param ghxh
     * @param czybm
     * @param ywckbm
     * @param brid
     * @param czyks
     * @param ylkh
     * @return
     */
    private String  ifithanone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh){
        registMapper.updejsjlywxh("000001",DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,"2","0",
                "2","0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        registMapper.saveghjsb(jsjlBean);


        return  jsjlid;
    }

    private String  ifilessone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh,String xh){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=registMapper.selectywxhbyid(xh1).getXh();
        registMapper.updejsjlywxh(xh2,DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+xh1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,"2","0",
                "2","0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        registMapper.saveghjsb(jsjlBean);
        return  jsjlid;
    }


    private  PayDetailBean setPayBean(PatientInfodetail pa,String jsxh,RegistPatientInfoRequest r,String czybm,String czyxm){
        PayDetailBean pay=new PayDetailBean();
        pay.setBzms("微信办卡结算");
        pay.setPayTag(r.getPayTag());
        pay.setCodeTermidNo(r.getCodeTermidNo());
        pay.setPayTranDate(r.getPayTranDate());
        pay.setAddress(pa.getAddress());
        pay.setAutoTranNo(r.getAutoTranNo());
        pay.setBankCardNo(r.getBankCardNo());
        pay.setBankPNo(r.getBankPNo());
        pay.setBankReferNo(r.getBankReferNo());
        pay.setBankTermid(r.getBankTermid());
        pay.setBirthday(pa.getBirthday());
        pay.setCardType(r.getCardType());
        pay.setCodeBuyerAccount(r.getCodeBuyerAccount());
        pay.setBankTranNo(r.getBankTranNo());
        pay.setJsxh(jsxh);
        pay.setCodeTradeOrderNo(r.getCodeTradeOrderNo());
        pay.setMobile(pa.getMobile());
        pay.setName(pa.getName());
        pay.setPayAmt(r.getPayAmt());
        pay.setIDCardNo(pa.getIDCardNo());
        pay.setSex(pa.getSex());
        pay.setUserId(r.getUserId());
        pay.setPayTranTime(r.getPayTranTime());
        pay.setCardNo(r.getCardNo());
        pay.setPatientID(pa.getPatientID());
        pay.setReqtype(r.getReqtype());
        pay.setCzybm(czybm);
        pay.setCzyxm(czyxm);
        return  pay;
    }


}

